﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFTray
{
	public partial class DlgManualTray : Form
	{
		internal FeedTray Feed;
		public DlgManualTray()
		{
			InitializeComponent();

			Load += (s, e) =>
			  {
				  if (Feed == null) return;
				  var cfg = Feed.GetCfgFeed();
				  Text = "操作面板 ---- " + cfg.Name;
			  };

			VisibleChanged += (s, e) =>
			  {
				  if (!Visible) return;
				  if (Feed == null) return;
				  UpdateTrayVF();
				  UpdateBinVF();
			  };

			indicatorLightFeedOver.Click += IndicatorLightClear_Click;
			indicatorLightBinOver.Click += IndicatorLightClear_Click;
			indicatorLightSysTemperature.Click += IndicatorLightClear_Click;
			indicatorLightMLTemperature.Click += IndicatorLightClear_Click;
			indicatorLightLoV.Click += IndicatorLightClear_Click;
			indicatorLightHiV.Click += IndicatorLightClear_Click;

			#region list add
			freqes.Add(textBoxFreqUp);
			vols.Add(textBoxVolUp);
			//times.Add(textBoxTimeUp);

			freqes.Add(textBoxFreqDown);
			vols.Add(textBoxVolDown);
			//times.Add(textBoxTimeDown);

			freqes.Add(textBoxFreqLeft);
			vols.Add(textBoxVolLeft);
			//times.Add(textBoxTimeLeft);

			freqes.Add(textBoxFreqRight);
			vols.Add(textBoxVolRight);
			//times.Add(textBoxTimeRight);

			freqes.Add(textBoxFreqLeftUp);
			vols.Add(textBoxVolLeftUp);
			//times.Add(textBoxTimeLeftUp);

			freqes.Add(textBoxFreqRightUp);
			vols.Add(textBoxVolRightUp);
			//times.Add(textBoxTimeRightUp);

			freqes.Add(textBoxFreqLeftDown);
			vols.Add(textBoxVolLeftDown);
			//times.Add(textBoxTimeLeftDown);

			freqes.Add(textBoxFreqRightDown);
			vols.Add(textBoxVolRightDown);
			//times.Add(textBoxTimeRightDown);

			freqes.Add(textBoxFreqVer);
			vols.Add(textBoxVolVer);
			//times.Add(textBoxTimeVer);

			freqes.Add(textBoxFreqHor);
			vols.Add(textBoxVolHor);
			//times.Add(textBoxTimeHor);

			freqes.Add(textBoxFreqZS);
			vols.Add(textBoxVolZS);
			//times.Add(textBoxTimeZS);

			freqes.Add(textBoxFreqJL_Ver);
			vols.Add(textBoxVolJL_Hor);
			//times.Add(textBoxTimeJL_Total);

			freqes.Add(textBoxFreqFM);
			vols.Add(textBoxVolFM);
			//times.Add(textBoxTimeFM);
			#endregion list add

			ftBinControl1.Buttons_Clicked += BinCon_ButtonClicked;

			buttonLeftUp.Tag = 4;
			buttonUp.Tag = 0;
			buttonRightUp.Tag = 5;
			buttonLeft.Tag = 2;
			buttonJL.Tag = 7;
			buttonRight.Tag = 3;
			buttonLeftDown.Tag = 6;
			buttonDown.Tag = 1;
			buttonRightDown.Tag = 8;
			buttonHor.Tag = 10;
			buttonVer.Tag = 9;
			buttonDS.Tag = 11;
			buttonFM.Tag = 12;
			//buttonGroup1.Tag = 13;
			//buttonGroup2.Tag = 14;
			buttonStop.Tag = 15;

			buttonLeftUp.Click += feedtray_CmdClick;
			buttonUp.Click += feedtray_CmdClick;
			buttonRightUp.Click += feedtray_CmdClick;
			buttonLeft.Click += feedtray_CmdClick;
			buttonJL.Click += feedtray_CmdClick;
			buttonRight.Click += feedtray_CmdClick;
			buttonLeftDown.Click += feedtray_CmdClick;
			buttonDown.Click += feedtray_CmdClick;
			buttonRightDown.Click += feedtray_CmdClick;
			buttonHor.Click += feedtray_CmdClick;
			buttonVer.Click += feedtray_CmdClick;
			buttonDS.Click += feedtray_CmdClick;
			buttonFM.Click += feedtray_CmdClick;
			//buttonGroup1.Click += feedtray_CmdClick;
			//buttonGroup2.Click += feedtray_CmdClick;
			buttonStop.Click += feedtray_CmdClick;

		}
		private List<Button> btns = new List<Button>();

		private void UpdateTrayVF()
		{
			if (Feed == null || Feed.Connected == false) return;
			if (Feed.ReadMotorFreqVTes())
			{
				var fvtes = Feed.FreqVtes;
				for (int i = 0; i < vols.Count; i++)
				{
					freqes[i].Text = (fvtes[i * 3 + 0] * 0.1F).ToString("F1");
					vols[i].Text = (fvtes[i * 3 + 1] * 0.1F).ToString("F1");
					//times[i].Text = (fvtes[i * 3 + 2] * 0.1F).ToString("F1");
				}
			}
			
		}
		private void UpdateBinVF()
		{
			if (Feed.ReadBinVoltFreq())
			{
				ftBinControl1.Volt = Feed.BinVolt;
				ftBinControl1.Freq = Feed.BinFreq;
			}
		}
		private void feedlight_Cmdscroll(object sender, EventArgs e)
		{
			TrackBar tra = sender as TrackBar;
			try
			{
				if (tra.Name == "trackBar_lightbrightA")
				{
					Feed.Brightness6_999(true, Convert.ToUInt16(tra.Value));
				}
				else if (tra.Name == "trackBar_lightbrightB")
				{
					Feed.Brightness6_999(false, Convert.ToUInt16(tra.Value));
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void feedlight_CmdClick(object sender, EventArgs e)
		{
			Button btn = sender as Button;
			try
			{
				if (btn.Name == "buootn_LightonA")
				{
					Feed.LightSwitch(true, true);

				}
				else if (btn.Name == "buootn_LightoffA")
				{
					Feed.LightSwitch(true, false);
				}
				else if (btn.Name == "buootn_LightonB")
				{
					Feed.LightSwitch(false, true);

				}
				else if (btn.Name == "buootn_LightoffB")
				{
					Feed.LightSwitch(false, false);

				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void IndicatorLightClear_Click(object sender, EventArgs e)
		{
			if (Feed==null ||Feed.Connected == false) return;
			ExCons.IndicatorLight ind = sender as ExCons.IndicatorLight;
			if (ind.ONFF == false) return;
			Feed.ClearError();
		}

		private List<TextBox> freqes = new List<TextBox>();
		private List<TextBox> vols = new List<TextBox>();
		//private List<TextBox> times = new List<TextBox>();

		private void BinCon_ButtonClicked(object sender, EventArgs e)
		{
			if (Feed == null || Feed.Connected == false) return;

			Button btn = sender as Button;
			int tag = Convert.ToInt32(btn.Tag);
			if (tag == 0)
			{
				if (ftBinControl1.BinOpeing) Feed.BinSwitch(false);
				else Feed.BinSwitch(true);
			}
			else if (tag == 1)
			{

				Feed.WriteBinVoltFreq(ftBinControl1.Volt, ftBinControl1.Freq);
				UpdateBinVF();
			}
		}


		private void feedtray_CmdClick(object sender, EventArgs e)
		{
			if (Feed == null || Feed.Connected == false) return;
			try
			{
				Button btn = sender as Button;
				ushort sendCmd = Convert.ToUInt16(Convert.ToInt32(btn.Tag));
				Feed.WriteMoveMode(sendCmd);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "ERROR");
			}
		}

		
		internal void OnUiTimer()
		{
			if (Visible == false) return;
			if (Feed == null) return;
			indicatorLightConnected.ONFF = Feed.Connected;
			indicatorLightFeedRuning.ONFF = Feed.S_FeedRuning;
			indicatorLightBinRuning.ONFF = Feed.S_BinRuning;
			indicatorLightLightA.ONFF = Feed.S_LightA;
			indicatorLightLightB.ONFF = Feed.S_LightB;
			indicatorLightFeedOver.ONFF = Feed.S_FeedOver;
			indicatorLightBinOver.ONFF = Feed.S_BinOver;
			indicatorLightSysTemperature.ONFF = Feed.S_Temperature;
			indicatorLightMLTemperature.ONFF = Feed.S_MotorLightTemperature;
			indicatorLightLoV.ONFF = Feed.S_LowVoltage;
			indicatorLightHiV.ONFF = Feed.S_HiVoltage;
			
			labelMoveMode.Text = "运动模式:" + Feed.Mode;
			ftBinControl1.BinOpeing = Feed.S_BinRuning;
		}

		private void buttonReadMove_Click(object sender, EventArgs e)
		{
			if (Feed == null || Feed.Connected == false) return;
			if (Feed.ReadMotorFreqVTes())
			{
				var fvtes = Feed.FreqVtes;
				for (int i = 0; i < vols.Count; i++)
				{
					freqes[i].Text = (fvtes[i * 3 + 0] * 0.1F).ToString("F1");
					vols[i].Text = (fvtes[i * 3 + 1] * 0.1F).ToString("F1");
					//times[i].Text = (fvtes[i * 3 + 2] * 0.1F).ToString("F1");
				}
			}
		}

		private void buttonWriteMove_Click(object sender, EventArgs e)
		{
			if (Feed == null || Feed.Connected == false) return;

			ushort[] fvtes = new ushort[39];
			for (int i = 0; i < freqes.Count; i++)
			{
				ushort freq = 0;
				ushort vol = 0;
				ushort tim = 0;
				try
				{
					freq = Convert.ToUInt16(Convert.ToSingle(freqes[i].Text) * 10);
				}
				catch (Exception ex)
				{
					freqes[i].Focus();
					freqes[i].SelectAll();
					MessageBox.Show(ex.Message,"ERROR");
					return;
				}

				try
				{
					vol = Convert.ToUInt16(Convert.ToSingle(vols[i].Text) * 10);
				}
				catch (Exception ex)
				{
					vols[i].Focus();
					vols[i].SelectAll();
					MessageBox.Show(ex.Message, "ERROR");
					return;
				}

				//try
				//{
				//	tim = Convert.ToUInt16(Convert.ToSingle(times[i].Text) * 10);
				//}
				//catch (Exception ex)
				//{
				//	times[i].Focus();
				//	times[i].SelectAll();
				//	MessageBox.Show(ex.Message, "ERROR");
				//	return;
				//}

				fvtes[i * 3 + 0] = freq;
				fvtes[i * 3 + 1] = vol;
				fvtes[i * 3 + 2] = tim;
			}

			Feed.WriteMotorFreqVTes(fvtes);
			UpdateTrayVF();
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			OnUiTimer();
		}
	}
}
